const DB = require('../../../utils/db')
const JwtUtil = require('../../../utils/jwt')
/**
 * POST
 * 编辑用户资料
 */

module.exports = async (ctx) => {
  let params = ctx.request.body
  // 获取token
  let token = ctx.request.header.authorization
  let jwt = new JwtUtil(token)
  // 用token获取到userid
  let userid = jwt.verifyToken()
  // 拼接参数
  let sql = ''
  for (let key in params) {
    sql = sql + `${key}='${params[key]}',`
  }
  // 更新个人信息
  // slice(0,-1) 从0到倒数第一个，左闭右开
  const res = await DB.query(
    `update user set ${sql.slice(0, -1)} where userid='${userid}'`
  )
  ctx.body = {
    code: 0,
    data: res,
  }
}
